﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Web.myadmin
{
    public partial class fanli_order : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //SessionHelp.AssertLogin(this); //登录验证

            #region 显示正确的布局
            string cmd = Request.QueryString["cmd"];
            switch (cmd)
            {
                case "add": initAddForm();break;
                case "edit":initEditFrom();break;
                case "view": initViewTable(); break;
                case "delete": delete(); break;
                default: showInfoList(); break;
            }
            showTheRightRegion(cmd);
            #endregion
        }

        #region 信息列表和查询
        /// <summary>
        /// 查询数据
        /// </summary>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            showInfoList();
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        protected void showInfoList()
        {
            #region 变量定义
            DataSet dset = new DataSet();
            fanli.DAL.fanli_order dal = new fanli.DAL.fanli_order();
            System.Collections.Generic.List<System.Data.SqlClient.SqlParameter> paraList = new List<System.Data.SqlClient.SqlParameter>();
            string dataFields = ""; //读取的字段名，空为所有
            string strWhere = "1=1"; //查询子句
            string strOrderBy = " id desc"; //排序参数，如果需要分页，则必填 
            #endregion

            #region 查询条件
            string strId = txtQryId.Text.Trim();
            if (strId != "" && strId != null )
            {
                strWhere += " and id=@id";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@id", strId));
            } 
            string strUserid = txtQryUserid.Text.Trim();
            if (strUserid != "" && strUserid != null )
            {
                strWhere += " and userid=@userid";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@userid", strUserid));
            } 
            //string strShopID = txtQryShopID.Text.Trim();
            //if (strShopID != "" && strShopID != null )
            //{
            //    strWhere += " and shopID=@shopID";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@shopID", strShopID));
            //} 
            //string strStatus = txtQryStatus.Text.Trim();
            //if (strStatus != "" && strStatus != null )
            //{
            //    strWhere += " and status=@status";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@status", strStatus));
            //} 
            //string strLianmeng = txtQryLianmeng.Text.Trim();
            //if (strLianmeng != "" && strLianmeng != null )
            //{
            //    strWhere += " and lianmeng=@lianmeng";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@lianmeng", strLianmeng));
            //} 
            //string strFanliMoney = txtQryFanliMoney.Text.Trim();
            //if (strFanliMoney != "" && strFanliMoney != null )
            //{
            //    strWhere += " and fanliMoney=@fanliMoney";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@fanliMoney", strFanliMoney));
            //} 
            //string strFanliStatus = txtQryFanliStatus.Text.Trim();
            //if (strFanliStatus != "" && strFanliStatus != null )
            //{
            //    strWhere += " and fanliStatus=@fanliStatus";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@fanliStatus", strFanliStatus));
            //} 
            //string strTrade_id = txtQryTrade_id.Text.Trim();
            //if (strTrade_id != "" && strTrade_id != null )
            //{
            //    strWhere += " and trade_id=@trade_id";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@trade_id", strTrade_id));
            //} 
            //string strPay_time = txtQryPay_time.Text.Trim();
            //if (strPay_time != "" && strPay_time != null )
            //{
            //    strWhere += " and pay_time=@pay_time";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@pay_time", strPay_time));
            //} 
            //string strCommission_rate = txtQryCommission_rate.Text.Trim();
            //if (strCommission_rate != "" && strCommission_rate != null )
            //{
            //    strWhere += " and commission_rate=@commission_rate";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@commission_rate", strCommission_rate));
            //} 
            //string strCommission = txtQryCommission.Text.Trim();
            //if (strCommission != "" && strCommission != null )
            //{
            //    strWhere += " and commission=@commission";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@commission", strCommission));
            //} 
            //string strOuter_code = txtQryOuter_code.Text.Trim();
            //if (strOuter_code != "" && strOuter_code != null )
            //{
            //    strWhere += " and outer_code=@outer_code";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@outer_code", strOuter_code));
            //} 
            //string strNum_iid = txtQryNum_iid.Text.Trim();
            //if (strNum_iid != "" && strNum_iid != null )
            //{
            //    strWhere += " and num_iid=@num_iid";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@num_iid", strNum_iid));
            //} 
            //string strItem_title = txtQryItem_title.Text.Trim();
            //if (strItem_title != "" && strItem_title != null )
            //{
            //    strWhere += " and item_title=@item_title";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@item_title", strItem_title));
            //} 
            //string strItem_num = txtQryItem_num.Text.Trim();
            //if (strItem_num != "" && strItem_num != null )
            //{
            //    strWhere += " and item_num=@item_num";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@item_num", strItem_num));
            //} 
            //string strPay_price = txtQryPay_price.Text.Trim();
            //if (strPay_price != "" && strPay_price != null )
            //{
            //    strWhere += " and pay_price=@pay_price";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@pay_price", strPay_price));
            //} 
            //string strReal_pay_fee = txtQryReal_pay_fee.Text.Trim();
            //if (strReal_pay_fee != "" && strReal_pay_fee != null )
            //{
            //    strWhere += " and real_pay_fee=@real_pay_fee";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@real_pay_fee", strReal_pay_fee));
            //} 
            //string strCategory_id = txtQryCategory_id.Text.Trim();
            //if (strCategory_id != "" && strCategory_id != null )
            //{
            //    strWhere += " and category_id=@category_id";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@category_id", strCategory_id));
            //} 
            //string strCategory_name = txtQryCategory_name.Text.Trim();
            //if (strCategory_name != "" && strCategory_name != null )
            //{
            //    strWhere += " and category_name=@category_name";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@category_name", strCategory_name));
            //} 
            //string strShop_title = txtQryShop_title.Text.Trim();
            //if (strShop_title != "" && strShop_title != null )
            //{
            //    strWhere += " and shop_title=@shop_title";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@shop_title", strShop_title));
            //} 
            //string strSeller_nick = txtQrySeller_nick.Text.Trim();
            //if (strSeller_nick != "" && strSeller_nick != null )
            //{
            //    strWhere += " and seller_nick=@seller_nick";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@seller_nick", strSeller_nick));
            //} 
            //string strExt1 = txtQryExt1.Text.Trim();
            //if (strExt1 != "" && strExt1 != null )
            //{
            //    strWhere += " and ext1=@ext1";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext1", strExt1));
            //} 
            //string strExt2 = txtQryExt2.Text.Trim();
            //if (strExt2 != "" && strExt2 != null )
            //{
            //    strWhere += " and ext2=@ext2";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext2", strExt2));
            //} 
            #endregion

             #region 分页控件绑定
             int pageSize = 30; //分页每页记录个数
             int totalRecourd = 0; //总记录个数
             int currentPage = 1; //当前页码
             try { currentPage = int.Parse(Request.QueryString["p"]); }
             catch { }

             totalRecourd = dal.GetCount(strWhere, paraList);
             dset = dal.GetDatasetByPageSql(currentPage, pageSize, strWhere, strOrderBy, paraList, dataFields);
             if (dset!=null && dset.Tables[0].Rows.Count > 0)
             {
                 rptInfoList.DataSource = dset;
                 rptInfoList.DataBind();
                 litPager.Text = LIB.pager.getPageHtml(this, totalRecourd, pageSize);
             }
             else
             {
                 litPromptMessage.Text = "没有查询到相关记录";
             } 
             #endregion
             #region 单页数据绑定
             //strWhere += " " + strOrderBy;
             //dset = dal.GetDataset(strWhere, paraList, dataFields);
             //if (dset.Tables[0].Rows.Count > 0)
             //{
             //    rptInfoList.DataSource = dset;
             //    rptInfoList.DataBind();
             //}
             //else
             //{
             //    litPromptMessage.Text = "没有查询到相关记录";
             //} 
             #endregion

        }
        #endregion

        #region 添加、修改、删除操作
        /// <summary>
        /// 添加修改记录
        /// </summary>
        protected void btnSumit_Click(object sender, EventArgs e)
        {
            #region 获取数据
            bool isAdd = true;
            int varId ;	//id
            int varUserid ;	//用户id
            int varShopID ;	//商城id
            int varStatus ;	//订单状态
            string varLianmeng;	//所属联盟
            float varFanliMoney ;	//返利佣金
            int varFanliStatus ;	//返利状态
            string varTrade_id;	//订单号
            DateTime varPay_time ;	//确认时间
            float varCommission_rate ;	//佣金比率
            float varCommission ;	//网站佣金
            string varOuter_code;	//推广渠道
            int varNum_iid ;	//商品ID
            string varItem_title;	//商品名称
            int varItem_num ;	//商品数量
            float varPay_price ;	//成交价格
            float varReal_pay_fee ;	//实际支付金额
            int varCategory_id ;	//商品类目ID
            string varCategory_name;	//商品类目名称
            string varShop_title;	//店铺名称
            string varSeller_nick;	//卖家昵称
            string varExt1;	//扩展1
            string varExt2;	//扩展2
            try
            {
               varId =int.Parse(txtId.Text.Trim());	//id
               varUserid =int.Parse(txtUserid.Text.Trim());	//用户id
               varShopID =int.Parse(txtShopID.Text.Trim());	//商城id
               varStatus =int.Parse(txtStatus.Text.Trim());	//订单状态
               varLianmeng =txtLianmeng.Text.Trim();	//所属联盟
               varFanliMoney =float.Parse(txtFanliMoney.Text.Trim());	//返利佣金
               varFanliStatus =int.Parse(txtFanliStatus.Text.Trim());	//返利状态
               varTrade_id =txtTrade_id.Text.Trim();	//订单号
               varPay_time =DateTime.Parse(txtPay_time.Text.Trim());	//确认时间
               varCommission_rate =float.Parse(txtCommission_rate.Text.Trim());	//佣金比率
               varCommission =float.Parse(txtCommission.Text.Trim());	//网站佣金
               varOuter_code =txtOuter_code.Text.Trim();	//推广渠道
               varNum_iid =int.Parse(txtNum_iid.Text.Trim());	//商品ID
               varItem_title =txtItem_title.Text.Trim();	//商品名称
               varItem_num =int.Parse(txtItem_num.Text.Trim());	//商品数量
               varPay_price =float.Parse(txtPay_price.Text.Trim());	//成交价格
               varReal_pay_fee =float.Parse(txtReal_pay_fee.Text.Trim());	//实际支付金额
               varCategory_id =int.Parse(txtCategory_id.Text.Trim());	//商品类目ID
               varCategory_name =txtCategory_name.Text.Trim();	//商品类目名称
               varShop_title =txtShop_title.Text.Trim();	//店铺名称
               varSeller_nick =txtSeller_nick.Text.Trim();	//卖家昵称
               varExt1 =txtExt1.Text.Trim();	//扩展1
               varExt2 =txtExt2.Text.Trim();	//扩展2
            }
            catch
            {
               LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
               return;
            }
            if (varId != 0) isAdd = false;
            #endregion

            #region 赋值model
            fanli.DAL.fanli_order dal = new fanli.DAL.fanli_order();
            fanli.Model.fanli_order model = new fanli.Model.fanli_order();
            if (!isAdd) model = dal.GetModel(varId);
            model.id = varId;	//id
            model.userid = varUserid;	//用户id
            model.shopID = varShopID;	//商城id
            model.status = varStatus;	//订单状态
            model.lianmeng = varLianmeng;	//所属联盟
            model.fanliMoney = varFanliMoney;	//返利佣金
            model.fanliStatus = varFanliStatus;	//返利状态
            model.trade_id = varTrade_id;	//订单号
            model.pay_time = varPay_time;	//确认时间
            model.commission_rate = varCommission_rate;	//佣金比率
            model.commission = varCommission;	//网站佣金
            model.outer_code = varOuter_code;	//推广渠道
            model.num_iid = varNum_iid;	//商品ID
            model.item_title = varItem_title;	//商品名称
            model.item_num = varItem_num;	//商品数量
            model.pay_price = varPay_price;	//成交价格
            model.real_pay_fee = varReal_pay_fee;	//实际支付金额
            model.category_id = varCategory_id;	//商品类目ID
            model.category_name = varCategory_name;	//商品类目名称
            model.shop_title = varShop_title;	//店铺名称
            model.seller_nick = varSeller_nick;	//卖家昵称
            model.ext1 = varExt1;	//扩展1
            model.ext2 = varExt2;	//扩展2
            #endregion

            #region 执行操作
            if (isAdd)
            {
                if (dal.Add(model) > 0)
                {
                    LIB.JsWrite.AlertAndGo(this, "添加成功", "?cmd=list");
                }
                else
                {
                    LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
                }
            }
            else
            {
                if (dal.Update(model) > 0)
                {
                    LIB.JsWrite.Alert(this, "修改成功");
                }
                else
                {
                    LIB.JsWrite.Alert(this, "操作失败");
                }
            } 
            #endregion
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        protected void delete()
        {
            #region 删除操作
            int id = 0;
            try { id = int.Parse(Request.QueryString["id"]); }
            catch
            {
                //litPromptMessage.Text = "非法的参数";
                LIB.JsWrite.Alert(this, "非法的参数");
                return;
            }

            fanli.DAL.fanli_order dal = new fanli.DAL.fanli_order();
            if (dal.Delete(id)>0)
            {
                //litPromptMessage.Text = "删除成功";
                LIB.JsWrite.AlertAndGo(this, "删除成功", "?cmd=list");
            }
            else
            {
                //litPromptMessage.Text = "操作失败";
                LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
            } 
            #endregion
        }
        #endregion

        #region 初始化添加表单
        /// <summary>
        /// 初始化添加表单
        /// </summary>
        protected void initAddForm()
        {
            lblFormTitle.Text = "添加信息";
            if (!IsPostBack)
            {

                txtUserid.Text = "";
                txtShopID.Text = "";
                txtStatus.Text = "";
                txtLianmeng.Text = "";
                txtFanliMoney.Text = "";
                txtFanliStatus.Text = "";
                txtTrade_id.Text = "";
                txtPay_time.Text = "";
                txtCommission_rate.Text = "";
                txtCommission.Text = "";
                txtOuter_code.Text = "";
                txtNum_iid.Text = "";
                txtItem_title.Text = "";
                txtItem_num.Text = "";
                txtPay_price.Text = "";
                txtReal_pay_fee.Text = "";
                txtCategory_id.Text = "";
                txtCategory_name.Text = "";
                txtShop_title.Text = "";
                txtSeller_nick.Text = "";
                txtExt1.Text = "";
                txtExt2.Text = "";
            }
        }
        #endregion

        #region 初始化修改表单
        /// <summary>
        /// 初始化修改表单
        /// </summary>
        protected void initEditFrom()
        {
            int id =Convert.ToInt32(Request.QueryString["id"]);
            lblFormTitle.Text = "修改信息";
            if (!IsPostBack)
            {
                fanli.Model.fanli_order model = new fanli.Model.fanli_order();
                fanli.DAL.fanli_order dal = new fanli.DAL.fanli_order();
                model = dal.GetModel(id);

                txtId.Text = model.id.ToString();
                txtUserid.Text = model.userid.ToString();
                txtShopID.Text = model.shopID.ToString();
                txtStatus.Text = model.status.ToString();
                txtLianmeng.Text = model.lianmeng.ToString();
                txtFanliMoney.Text = model.fanliMoney.ToString();
                txtFanliStatus.Text = model.fanliStatus.ToString();
                txtTrade_id.Text = model.trade_id.ToString();
                txtPay_time.Text = model.pay_time.ToString();
                txtCommission_rate.Text = model.commission_rate.ToString();
                txtCommission.Text = model.commission.ToString();
                txtOuter_code.Text = model.outer_code.ToString();
                txtNum_iid.Text = model.num_iid.ToString();
                txtItem_title.Text = model.item_title.ToString();
                txtItem_num.Text = model.item_num.ToString();
                txtPay_price.Text = model.pay_price.ToString();
                txtReal_pay_fee.Text = model.real_pay_fee.ToString();
                txtCategory_id.Text = model.category_id.ToString();
                txtCategory_name.Text = model.category_name.ToString();
                txtShop_title.Text = model.shop_title.ToString();
                txtSeller_nick.Text = model.seller_nick.ToString();
                txtExt1.Text = model.ext1.ToString();
                txtExt2.Text = model.ext2.ToString();
            }
        }
        #endregion

        #region 初始化查看表格
        /// <summary>
        /// 初始化查看表格
        /// </summary>
        protected void initViewTable()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            lblViewTitle.Text = "查看信息";
            if (!IsPostBack)
            {
                fanli.Model.fanli_order model = new fanli.Model.fanli_order();
                fanli.DAL.fanli_order dal = new fanli.DAL.fanli_order();
                model = dal.GetModel(id);

                lblUserid.Text = model.userid.ToString();
                lblShopID.Text = model.shopID.ToString();
                lblStatus.Text = model.status.ToString();
                lblLianmeng.Text = model.lianmeng.ToString();
                lblFanliMoney.Text = model.fanliMoney.ToString();
                lblFanliStatus.Text = model.fanliStatus.ToString();
                lblTrade_id.Text = model.trade_id.ToString();
                lblPay_time.Text = model.pay_time.ToString();
                lblCommission_rate.Text = model.commission_rate.ToString();
                lblCommission.Text = model.commission.ToString();
                lblOuter_code.Text = model.outer_code.ToString();
                lblNum_iid.Text = model.num_iid.ToString();
                lblItem_title.Text = model.item_title.ToString();
                lblItem_num.Text = model.item_num.ToString();
                lblPay_price.Text = model.pay_price.ToString();
                lblReal_pay_fee.Text = model.real_pay_fee.ToString();
                lblCategory_id.Text = model.category_id.ToString();
                lblCategory_name.Text = model.category_name.ToString();
                lblShop_title.Text = model.shop_title.ToString();
                lblSeller_nick.Text = model.seller_nick.ToString();
                lblExt1.Text = model.ext1.ToString();
                lblExt2.Text = model.ext2.ToString();
            }
        }

        #endregion

        #region 自定义函数

        /// <summary>
        /// 函数功能：示例函数
        /// 作者：buzhizhe 创建时间：2011.6.22
        /// 修改者：buzhizhe 最后修改时间：2011.6.22
        /// </summary>
        protected void YourFunction()
        {
            ///
        }


        #endregion


        #region 公共函数，不需要修改

        /// <summary>
        /// 根据cmd参数判断应该显示哪些部分内容
        /// </summary>
        /// <param name="cmd"></param>
        protected void showTheRightRegion(string cmd)
        {
            #region show or hide div
            switch (cmd)
            {
                case "add":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "edit":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "view":
                    divView.Visible = true;
                    divForm.Visible = false;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "delete":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
                case "query":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
                default:
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
            }
            #endregion
        }
        #endregion



    }


}

